package cn.iocoder.yudao.module.wine.dal.mysql.supplymarket;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.wine.controller.app.supplymarket.vo.AppSupplyMarketPageReqVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplymarket.SupplyMarketDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.admin.supplymarket.vo.*;

/**
 * 酒类行情管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface SupplyMarketMapper extends BaseMapperX<SupplyMarketDO> {

    default PageResult<SupplyMarketDO> selectPage(SupplyMarketPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SupplyMarketDO>()
                .eqIfPresent(SupplyMarketDO::getInfoType, reqVO.getInfoType())
                .likeIfPresent(SupplyMarketDO::getInfoNme, reqVO.getInfoNme())
                .eqIfPresent(SupplyMarketDO::getInfoTitle, reqVO.getInfoTitle())
                .eqIfPresent(SupplyMarketDO::getShowYear, reqVO.getShowYear())
                .eqIfPresent(SupplyMarketDO::getFragranceType, reqVO.getFragranceType())
                .eqIfPresent(SupplyMarketDO::getAveragePrice, reqVO.getAveragePrice())
                .eqIfPresent(SupplyMarketDO::getSalePrice, reqVO.getSalePrice())
                .eqIfPresent(SupplyMarketDO::getDescription, reqVO.getDescription())
                .eqIfPresent(SupplyMarketDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SupplyMarketDO::getSort, reqVO.getSort())
                .eqIfPresent(SupplyMarketDO::getIsTop, reqVO.getIsTop())
                .betweenIfPresent(SupplyMarketDO::getCountTime, reqVO.getCountTime())
                .betweenIfPresent(SupplyMarketDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SupplyMarketDO::getId));
    }

    default List<SupplyMarketDO> getSupplyMarketList(AppSupplyMarketPageReqVO reqVO){
        String keywords = reqVO.getKeywords();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split(" ");
            List<SupplyMarketDO> list = new ArrayList<>();
            for (String s : split) {
                List<SupplyMarketDO> supplyMarketDOS = selectList(new LambdaQueryWrapperX<SupplyMarketDO>()
                        .eqIfPresent(SupplyMarketDO::getFragranceType, reqVO.getFragranceType())
                        .eqIfPresent(SupplyMarketDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                        .eqIfPresent(SupplyMarketDO::getInfoType, reqVO.getInfoType())
                        .eqIfPresent(SupplyMarketDO::getPacking, reqVO.getPacking())
                        .eqIfPresent(SupplyMarketDO::getInfoNme, reqVO.getInfoNme())
                        .betweenIfPresent(SupplyMarketDO::getCountTime,reqVO.getCountTime())
                        .likeIfPresent(SupplyMarketDO::getInfoNme, s)
                        .likeIfPresent(SupplyMarketDO::getInfoTitle, s)
                        .likeIfPresent(SupplyMarketDO::getShowYear, s)
                        .likeIfPresent(SupplyMarketDO::getAveragePrice, s)
                        .likeIfPresent(SupplyMarketDO::getSalePrice, s)
                        .likeIfPresent(SupplyMarketDO::getDescription, s)
                );
                list.addAll(supplyMarketDOS);
            }
            if (!CollectionUtil.isEmpty(list)) {
                return list.stream().distinct().collect(Collectors.toList());
            }
            return list;
        }
        return selectList(new LambdaQueryWrapperX<SupplyMarketDO>()
                .eqIfPresent(SupplyMarketDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                .eqIfPresent(SupplyMarketDO::getInfoNme, reqVO.getInfoNme())
                .eqIfPresent(SupplyMarketDO::getFragranceType,reqVO.getFragranceType())
                .eqIfPresent(SupplyMarketDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyMarketDO::getPacking, reqVO.getPacking())
                .eqIfPresent(SupplyMarketDO::getShowYear, reqVO.getShowYear()) //JACKCHEN
                .eqIfPresent(SupplyMarketDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                .betweenIfPresent(SupplyMarketDO::getCountTime,reqVO.getCountTime())
                .eqIfPresent(SupplyMarketDO::getInfoType, reqVO.getInfoType()));
    }
}
